package com.cl.pattern.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Auther: dan gao
 * @Description:
 * @Date: 20:38 2018/1/16 0016
 */
public class CglibProxy implements MethodInterceptor {

    public Object getInstance(Class<?> clazz) throws  Exception{

        Enhancer enhancer = new Enhancer();
        //要把哪个设置为即将生成的新类父类
        enhancer.setSuperclass(clazz);

        enhancer.setCallback(this);

        return  enhancer.create();

    }


    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        //业务的增强
        System.out.println("1、寻找房源");
        Object invoke = methodProxy.invokeSuper(o, objects);
        System.out.println("2、协助房管局备案");
        return invoke;
    }
}